package com.baizhi.service.impl;

import com.baizhi.config.redis.AddCacheAnnotation;
import com.baizhi.config.redis.DeleteCacheAnnotation;
import com.baizhi.config.log.LogAnnotation;
import com.baizhi.entity.Guru;
import com.baizhi.mapper.GuruMapper;
import com.baizhi.service.GuruService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class GuruServiceImpl implements GuruService {
    @Autowired
    private GuruMapper guruMapper;

    @Transactional()
    @LogAnnotation(content = "展示上师",type = "select")
    @AddCacheAnnotation
    public Map selectPage(Integer pageNum, Integer pageSize) {
        Map map = new HashMap();
        Page<Guru> page = new Page<>(pageNum, pageSize);
        Page<Guru> guruPage = guruMapper.selectPage(page, null);
        List<Guru> gurus = guruPage.getRecords();
        long count = guruPage.getTotal();
        map.put("code",0);
        map.put("data",gurus);
        map.put("count",count);
        return map;
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @LogAnnotation(content = "添加上师",type = "insert")
    @DeleteCacheAnnotation
    public void insert(Guru guru) {
        try{
            guruMapper.insert(guru);
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @LogAnnotation(content = "删除上师",type = "delete")
    @DeleteCacheAnnotation
    public void deleteById(Integer id) {
        try{
            guruMapper.deleteById(id);
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @LogAnnotation(content = "更新上师",type = "update")
    @DeleteCacheAnnotation
    public void update(Guru guru) {
        try{
            guruMapper.updateById(guru);
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Transactional()
    @AddCacheAnnotation
    public Guru selectById(Integer id) {

        Guru guru = guruMapper.selectById(id);
        return guru;
    }
}
